/**
 * 系统设置 员工管理
 */
<template>
  <div>
          
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">

      <el-form-item label="">
        <el-input size="small" v-model="formInline.name" placeholder="输入科室名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column sortable prop="id" label="序号" width="80">
      </el-table-column>
      <el-table-column sortable prop="number" label="科室编号" width="100">
      </el-table-column>
      <el-table-column sortable prop="name" label="科室" width="80">
      </el-table-column>
      <el-table-column sortable prop="description" label="简介" width="200">
      </el-table-column>      
      <el-table-column sortable prop="createTime" label="创建时间" width="180">        
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteOper(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="部门编号" prop="number">
          <el-input-number :min="100" :max="999" size="small" v-model="editForm.number" auto-complete="off" placeholder="请输入部门编号"></el-input-number>
        </el-form-item>
        <el-form-item label="科室名" prop="name">
          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入科室名"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input size="small" v-model="editForm.description" auto-complete="off" placeholder="请输入描述"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <!-- 如果是添加则显示添加按钮，点击事件对应添加方法 -->
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitInsertForm('editForm')" v-if="title=='添加'">添加</el-button>
        <!-- 如果是修改则显示修改按钮，点击事件对应修改方法 -->
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitUpdateForm('editForm')" v-if="title=='修改'">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deptList, deptSave, deptDelete } from '../../../api/userMG'
import Pagination from '../../../components/Pagination'
import axios from 'axios'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        id:'',
        number: '',
        name: '',
        description: '',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        number: [{ required: true, message: '请输入科室编号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入科室名', trigger: 'blur' }],
        description:[{required: true, message: '请输入科室描述', trigger: 'blur'}]
      },
      formInline: {
        page: 1,
        limit: 10,
        officeId:'',
        name: '',
        token: localStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }      
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 创建完毕获取员工列表
   */
  created() {
    this.getdata(this.formInline);    
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取员工列表
    getdata(parameter) {
      this.loading = true      
      axios.post('/api/office/selectAllOffice?pageNum='+parameter.page+'&pageSize='+parameter.limit,{'name':parameter.name})
        .then(res => {
          this.loading = false          
          this.listData = res.data.data.list
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page
          this.pageparm.pageSize = this.formInline.limit
          this.pageparm.total = res.data.data.total          
        })
        .catch(err => {
          this.loading = false
          this.$message.error('加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.formInline.page=1
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.id=row.id
        this.editForm.number = row.number
        this.editForm.name = row.name
        this.editForm.description = row.description
      } else {
        this.title = '添加'
        this.editForm.deptId = ''
        this.editForm.deptName = ''
        this.editForm.deptNo = ''
      }
    },
    // 新增弹窗的保存方法
    submitInsertForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          axios.post('/api/office/insertOneOffice',this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              console.log('插入的结果是',res)
                this.getdata(this.formInline)
              if (res.data.code==200) {
                this.$message({
                  type: 'success',
                  message: '保存成功！'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 更新弹窗的保存方法
    submitUpdateForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          axios.post('/api/office/updateOfficeById',this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              console.log('更新的结果是',res)
                this.getdata(this.formInline)
              if (res.data.code==200) {
                this.$message({
                  type: 'success',
                  message: '更新成功！'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('更新失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除员工
    deleteOper(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          axios.post('/api/office/deleteOfficeById?id='+row.id)
          // deptDelete(row.deptId)
            .then(res => {
                this.getdata(this.formInline)
              if (res.data.code==200) {
                this.$message({
                  type: 'success',
                  message: '已删除!'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 